package com.android.signhelper.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by ikata on 2018/11/14.
 */

public class MyDatabaseHelper extends SQLiteOpenHelper {
    private final String CREATE_STUDENT = "create table student(" +
            "userName integer primary key," +
            "name text," +
            "token text," +
            "password text," +
            "signinfoId text," +
            "message text," +
            "foreign key (signinfoId) references signinfo(devUuid))";
    private final String CREATE_SIGNINFO = "create table signinfo(" +
            "devUuid text primary key," +
            "bleId text," +
            "signId text," +
            "available integer)";

    public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_SIGNINFO);
        db.execSQL(CREATE_STUDENT);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        if (!db.isReadOnly()) {
            // Enable foreign key constraints 开启外键约束
            db.execSQL("PRAGMA foreign_keys=ON;");
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table  if exists student");
        db.execSQL("drop table  if exists signinfo");
        onCreate(db);
    }
}
